<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends CI_Model {

    public $tablename = 'hpg_user';
    
    public $tab_auth = 'hpg_user_auth';
    
    public function insert($data){
        if( !$data ){
            return false;
        }
        $res = $this->db->insert($this->tablename, $data);
        return $this->db->insert_id();
    }
    
    
    public function getUserInfo($where){
        if( !$where ){
            return false;
        }
        $data = $this->db->from($this->tablename)->select('id,user_name,photo,corp_id,group_id')->where($where)->fetchOne();
        return $data ? $data : null;
    }
    
    public function getUserByUids($uids){
        if( !$uids ){
            return false;
        }
        $list = $this->db->from($this->tablename)->select('id,user_name,photo')->where_in('id', $uids)->get_array();
        return $list;
    }
    
    public function getUserByUnionid($unionid){
        if( !$unionid ){
            return false;
        }
        $list = $this->db->from($this->tablename)->select('id,user_name,photo')->where('unionid', $unionid)->fetchOne();
        return $list;
    }
    
    public function getAuthInfo($uid){
        if( !$uid ){
            return false;
        }
        $data = $this->db->from($this->tab_auth)->select('*')->where('uid', $uid)->fetchOne();
        return $data ? $data : null;
    }
    
    public function userAuth($uids){
        if( !$uids || !is_array($uids)){
            return false;
        }
        $list = $this->db->from($this->tablename.' as a')
        ->select('a.id,a.photo,b.real_name,job,institution,city,description')->join($this->tab_auth.' as b', 'a.id=b.uid', 'left')->where_in('a.id', $uids)->get_array();
        $return = [];
        foreach ($list as $v){
            $return[$v['id']] = $v;
        }
        return $return ? $return : [];
    }
    
    public function insertAuth($data){
        $res = $this->db->insert($this->tab_auth, $data);
        return $this->db->insert_id();
    }
    
    public function updateAuth($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tab_auth, $data, $map);
        return $res;
    }
}
?>